# -*- coding: utf-8 -*-
"""
-------------------------------------------------
# @Project  :pythonProject
# @File     :violator_cash
# @Date     :2021/8/26 17:41
# @Author   :zhumenglong
# @Software :PyCharm
-------------------------------------------------
"""

from wareHouse import *

class Cash:

    def __init__(self,file_path,Index_path,goal_path):
        self.file_path = file_path
        self.Index_path = Index_path
        self.goal_path = goal_path

    def draw_frame(self):
        cash,person = 0,0
        with open(self.Index_path,'r',encoding='utf-8') as fp:
            while True:
                file_name = fp.readline()[:-1]
                if len(file_name) > 0:
                    json_name = self.file_path+file_name+'.json'
                    pict_name = self.file_path+file_name+'.jpg'
                    with open(json_name,'rb') as f:
                        # f.read()
                        json_info = json.loads(f.read())
                        img_info = method.cv2_imread(pict_name,-1)
                        for i in json_info:
                            if i == "ped_box":
                                cat_name = "person"
                                for num in json_info[i]:
                                    xmin = int(num['x'])
                                    ymin = int(num['y'])
                                    xmax = int(num['x']) + int(num['width'])
                                    ymax = int(num['y']) + int(num['height'])

                                    x = 0
                                    y = 250
                                    z = 0
                                    color = (x,y,z)
                                    cv2.rectangle(img_info, (int(xmin), int(ymin)), (int(xmax), int(ymax)), color, 2)
                                    font = cv2.FONT_HERSHEY_SIMPLEX
                                    cv2.putText(img_info, cat_name, (int(xmin), int(ymin)), font, 1, color, 2)
                            elif i == "cash_box":
                                cat_name = "cash_box"
                                for num in json_info[i]:
                                    # print(type(num))
                                    # print(num)
                                    xmin = int(num['x'])
                                    ymin = int(num['y'])
                                    xmax = int(num['x']) + int(num['width'])
                                    ymax = int(num['y']) + int(num['height'])
                                    conf = float(num['conf'])
                                    conf = str(conf)

                                    x = 0
                                    y = 0
                                    z = 250
                                    color = (x, y, z)
                                    cv2.rectangle(img_info, (int(xmin), int(ymin)), (int(xmax), int(ymax)), color, 2)
                                    font = cv2.FONT_HERSHEY_SIMPLEX
                                    cv2.putText(img_info, cat_name, (int(xmin), int(ymin)), font, 1, color, 2)
                                    cv2.putText(img_info, conf, (int(xmax)+10, int(ymax)+30), font, 1, color, 2)
                            else:
                                cat_name = str(json_info[i])
                                x = 250
                                y = 0
                                z = 0
                                color = (x, y, z)
                                # cv2.rectangle(img_info, (int(xmin), int(ymin)), (int(xmax), int(ymax)), color, 2)
                                font = cv2.FONT_HERSHEY_SIMPLEX
                                cv2.putText(img_info, cat_name, (1600,100), font, 2, color, 2)

                        method.create_path(self.goal_path)

                        method.cv2_imwrite_jpg("./" + self.goal_path + "/" + file_name + ".jpg", img_info)
                else:
                    break
